﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using NanoERP.Model;
using NanoERP.Common;

namespace NanoERP.BaseInfo
{
    [Module("FrmBillTypeInfo")]
    public partial class FrmBillTypeInfo : ModuleForm
    {
        readonly DateTime _data = DateTime.Parse("2013-07-01");
        readonly BilltypeinfoBLL _billtypeinfoBLL;
        Billtypeinfo _billtypeinfo;
        public FrmBillTypeInfo()
        {
            InitializeComponent();

            _billtypeinfoBLL = new BilltypeinfoBLL();

            cboCodesep1.SelectedIndex = 0;
            cboCodedate.SelectedIndex = 1;
            cboCodesep2.SelectedIndex = 0;
            cboCodesno.SelectedIndex = 2;

            QueryInfo();
        }

        /// <summary>
        /// 
        /// </summary>
        void QueryInfo()
        {
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = _billtypeinfoBLL.Query();

            dgvList_CellClick(dgvList, new DataGridViewCellEventArgs(1, 0));
        }

        void FormatData(object sender, EventArgs e)
        {
            var dateFormat = string.Empty;
            switch (cboCodedate.SelectedIndex)
            {
                case 0: dateFormat = _data.ToString("yyyy-MM-dd"); break;
                case 1: dateFormat = _data.ToString("yyyyMMdd"); break;
                case 2: dateFormat = _data.ToString("yyyy年MM月dd日"); break;
                case 3: dateFormat = _data.ToString("MM-dd"); break;
                case 4: dateFormat = _data.ToString("MMdd"); break;
                case 5: dateFormat = _data.ToString("MM月dd日"); break;
                case 6: dateFormat = _data.ToString("yyyy-MM"); break;
                case 7: dateFormat = _data.ToString("yyyyMM"); break;
                case 8: dateFormat = _data.ToString("yyyy年MM月"); break;
                case 9: dateFormat = _data.ToString("yyyy"); break;
                case 10: dateFormat = _data.ToString("yyyy年"); break;
                default: dateFormat = "-"; break;
            }

            var split1 = cboCodesep1.Text.Equals("无") ? "" : cboCodesep1.Text;
            var split2 = cboCodesep2.Text.Equals("无") ? "" : cboCodesep2.Text;

            lbCodesample.Text = string.Format("{0}{1}{2}{3}{4}", txtCodepre.Text, split1, dateFormat, split2, cboCodesno.Text);
        }

        void SetSelectedIndex(int dataIndex)
        {
            cboCodesep1.SelectedIndex = 0;
            cboCodedate.SelectedIndex = dataIndex;
            cboCodesep2.SelectedIndex = 0;
            cboCodesno.SelectedIndex = 2;
        }

        private void rbExp2_CheckedChanged(object sender, EventArgs e)
        {
            if (rbExp2.Checked) SetSelectedIndex(1);
        }

        private void rbExp1_CheckedChanged(object sender, EventArgs e)
        {
            if (rbExp1.Checked) SetSelectedIndex(0);
        }

        private void rbExp3_CheckedChanged(object sender, EventArgs e)
        {
            if (rbExp3.Checked) SetSelectedIndex(7);
        }

        private void rbExp4_CheckedChanged(object sender, EventArgs e)
        {
            if (rbExp4.Checked) SetSelectedIndex(4);
        }

        private void btnSaveBill_Click(object sender, EventArgs e)
        {
            BindFormContro(_billtypeinfo);
            _billtypeinfoBLL.Edit(_billtypeinfo);
            MessageBox.Show("当前单据规则已成功保存.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnAllBills_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow d in dgvList.Rows)
            {
                var o = d.DataBoundItem as Billtypeinfo;
                BindFormContro(o);
                _billtypeinfoBLL.Edit(o);
            }

            MessageBox.Show("当前单据规则已成功应用到所有业务单据.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnBill0_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow d in dgvList.Rows)
            {
                var o = d.DataBoundItem as Billtypeinfo;
                o.Maxbillno = 0;
                BindFormContro(o);
                _billtypeinfoBLL.Edit(o);
            }
            dgvList.Refresh();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dgvList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;

            _billtypeinfo = dgvList.Rows[e.RowIndex].DataBoundItem as Billtypeinfo;

            billTypeName.Text = _billtypeinfo.Billname;
            txtCodepre.Text = _billtypeinfo.Codepre;
            cboCodesep1.Text = _billtypeinfo.Codesep1;
            cboCodedate.Text = _billtypeinfo.Codedate;
            cboCodesep2.Text = _billtypeinfo.Codesep2;
            cboCodesno.Text = _billtypeinfo.Codesno;
            lbCodesample.Text = _billtypeinfo.Codesample;
        }

        void BindFormContro(Billtypeinfo o)
        {
            o.Codepre = txtCodepre.Text;
            o.Codesep1 = cboCodesep1.Text;
            o.Codedate = cboCodedate.Text;
            o.Codesep2 = cboCodesep2.Text;
            o.Codesno = cboCodesno.Text;
            o.Codesample = lbCodesample.Text;
        }

        private void tsmSetCurrRow_Click(object sender, EventArgs e)
        {
            var o = dgvList.CurrentRow.DataBoundItem as Billtypeinfo;
            var f = new FrmSetBillNum(o.Maxbillno.Value.ToString());
            if (f.ShowDialog() == DialogResult.OK)
            {
                o.Maxbillno = Normal.ToInt(f.Tag);
                _billtypeinfoBLL.Edit(o);
                dgvList.Refresh();
            }
        }

        private void tsmSaveBill_Click(object sender, EventArgs e)
        {
            btnSaveBill_Click(sender, e);
        }

        private void tsmRestAllBill_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow d in dgvList.Rows)
            {
                var o = d.DataBoundItem as Billtypeinfo;
                o.Maxbillno = 0;
                o.Codesep1 = "-";
                o.Codedate = "YYYYMMDD";
                o.Codesep2 = "-";
                o.Codesno = "0001";
                o.Codesample = string.Format("{0}-20130701-0001", o.Codepre);

                _billtypeinfoBLL.Edit(o);
            }
            dgvList.Refresh();
        }
    }
}
